package mobi.hami.huapin.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.model.UserInfo;
import mobi.hami.huapin.model.UserWechat;
import mobi.hami.huapin.service.*;
import mobi.hami.huapin.util.HttpUtil;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * Create by 子龙 on 2018/8/28.
 */
@RestController
@RequestMapping(value = "/user", produces = "application/json")
@Api(description = "用户api")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserWechatService userWechatService;

    @Autowired
    private UserInvoiceService userInvoiceService;

    @Autowired
    private ReturnGoodsService returnGoodsService;

    @Autowired
    private WxauthorizeService wxauthorizeService;

    /**
     * 获取授权信息
     * @param
     * @return
     */
    @RequestMapping(value = "/wxauthorize/{loginCode}", method = RequestMethod.GET)
    @ApiOperation(value = "用户授权信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "loginCode", value = "授权code", example = "oEoCV5OzMI4wZELW4QWfi2iJ8x4o", paramType = "path", required = true, dataType = "String"),
            }
    )
    public Result wxauthorize(@PathVariable String loginCode) {
        return wxauthorizeService.wxauthorize(loginCode);
    }

    /**
     * 修改用户手机号
     * @param userId
     * @param phone
     * @return
     */
    @RequestMapping(value = "/updatephone.do", method = RequestMethod.POST)
    @ApiOperation(value = "修改用户手机号", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", example = "9", paramType = "query", required = true, dataType = "Integer"),
                    @ApiImplicitParam(name = "phone", value = "手机号码", example = "18725940122", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result updatePhone(Integer userId,String phone){
        return userInfoService.updatePhone(userId,phone);
    }

    /**
     *补全用户信息
     * @param reqMap
     * @return
     */
    @RequestMapping(value = "/completuserinfo.do", method = RequestMethod.POST)
    @ApiOperation(value = "补全用户信息", notes = " ")
    public Result completUserInfo(
            @RequestBody
            @ApiParam(value ="{\n" +
            "\"openid\":\"oEoCV5OzMI4wZELW4QWfi2iJ8x4o\",\n" +
            "\"userName\":\"zbb\",\n" +
            "\"nickName\":\"子龙\",\n" +
            "\"phone\":\"18725917011\",\n" +
            "\"avatarUrl\":\"www.baidu.com/a.png\",\n" +
            "\"gender\":1,\n" +
            "\"city\":\"北京\",\n" +
            "\"province\":\"北京市\",\n" +
            "\"country\":\"北京省\",\n" +
            "\"unionId\":\"1111\"\n" +
            "}") Map<String, Object> reqMap){

        if(null == reqMap.get("openid")){
            return Results.failure();
        }
        userWechatService.completUserInfo(reqMap);
        return Results.success();
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping(value = "/getuserinfo.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "openId", value = "用户openId", example = "oEoCV5OzMI4wZELW4QWfi2iJ8x4o", paramType = "query", required = true, dataType = "String"),
            }
    )
    public Result getUserInfo(String openId){
        UserWechat uwh = userWechatService.selectOneByWechatSmallId(openId);
        UserInfo ufo = userInfoService.selectOnebyId(uwh.getUser_id());
        if(null == uwh || null == ufo){
            return Results.failure("未查询到该用户信息!");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userInfo",ufo);
        map.put("userWechat",uwh);
        return Results.successWithData(map, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    /**
     *用户申请发票
     * @param reqMap
     * @return
     */
    @RequestMapping(value = "/userinvoice.do", method = RequestMethod.POST)
    @ApiOperation(value = "用户申请发票", notes = "order_id:订单主键,user_id:用户主键,open_id:用户open_id,type:发票类型0个人/非企业,1企业单位,ein:税号,企业发票才有税号," +
            "content:发票内容,money:发票金额,contact_name:发票收货人称谓,contact_mobile:发票收货人手机号,contact_address:发票收货人地址")
    public Result CreateUserInvoice(
            @RequestBody
            @ApiParam(value ="{\n" +
                    "  \"order_id\": 38,\n" +
                    "  \"user_id\": 9,\n" +
                    "  \"open_id\": \"oEoCV5OzMI4wZELW4QWfi2iJ8x4o\",\n" +
                    "  \"type\": 0,\n" +
                    "  \"ein\": \"我是税号\",\n" +
                    "  \"content\": \"发票的我是内容\",\n" +
                    "  \"head\": \"发票的抬头\",\n" +
                    "  \"money\": \"486.02\",\n" +
                    "  \"contact_name\": \"赵云\",\n" +
                    "  \"contact_mobile\": \"18725941011\",\n" +
                    "  \"contact_address\": \"北京朝阳xxx街xx号\"\n" +
                    "}") Map<String,Object> reqMap){
        if(null == reqMap || reqMap.isEmpty()){
            return  Results.failure();
        }
        return userInvoiceService.createUserInvoice(reqMap);
    }


    /**
     * 获取用户的订单是否已开发票
     */
    @RequestMapping(value = "/existinvoice.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户的订单是否已开发票", notes = "data:0标识申请中,1已经同意,已开且线下运送中2用户已收,发票完成,99未开过发票")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "user_id", value = "用户user_id", example = "3000001", paramType = "query", required = true, dataType = "int"),
                    @ApiImplicitParam(name = "openId", value = "用户openId", example = "1111xs1sddddss", paramType = "query", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "order_id", value = "订单id", example = "266", paramType = "query", required = true, dataType = "int"),
            }
    )
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": true,\n" +
                    "  \"code\": \"1\",\n" +
                    "  \"msg\": \"该订单未开过发票\"\n" +
                    "}")
    })
    public Result existUserInvoice(Integer user_id,Integer order_id,String openId){
        return userInvoiceService.existInvoice(user_id,order_id,openId);
    }



    /**
     *用户退货申请
     * @param reqMap
     * @return
     */
    @RequestMapping(value = "/returngoods.do", method = RequestMethod.POST)
    @ApiOperation(value = "用户退货申请", notes = "order_id:订单主键,user_id:用户主键,open_id:用户open_id,reason:退货原因,imgs:退货时用户上传图片的链接地址,多个请用英文逗号分割")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": false,\n" +
                    "  \"status\": 0添加退货信息成功1添加退货信息失败2user_id为空3open_id为空4该用户不存在5用户信息不一致6order_id为空7退货原因为空8图片为空9该订单不存在" +
                    "10用户信息不一致暂不支持退货11该订单未收货或者派发,暂不支持退货12该订单已申请过退货13该订单不在退货时间内\n" +
                    "  \"msg\": \"添加退货信息成功\"\n" +
                    "}")
    })
    public Result ReturnGoods(
            @RequestBody
            @ApiParam(value ="{\n" +
                    "  \"order_id\": 38,\n" +
                    "  \"user_id\": 9,\n" +
                    "  \"open_id\": \"oEoCV5OzMI4wZELW4QWfi2iJ8x4o\",\n" +
                    "  \"reason\": \"我是退货原因\",\n" +
                    "  \"imgs\": \"我是图片们,记得多个图片用,号分割我哦\"\n" +
                    "}") Map<String,Object> reqMap){
        if(null == reqMap || reqMap.isEmpty()){
            return  Results.failure();
        }
        return returnGoodsService.AddreturnGoods(reqMap);
    }





    /**
     *如果退货申请通过则继续填写退货方式信息
     * @param reqMap
     * @return
     */
    @RequestMapping(value = "/updatereturngoods.do", method = RequestMethod.POST)
    @ApiOperation(value = "填写退货方式信息", notes = "订单the_order_id,用户主键user_id,用户open_id,快递机构主键courier_id,快递订单号courier_code,退货联系人称谓contact_name,退货联系人电话contact_mobile")
    @ApiResponses({
            @ApiResponse(code = 200,message = "{\n" +
                    "  \"success\": false,\n" +
                    "  \"status\": 0操作成功1操作失败2退货申请记录id为空3无退货申请记录4退货申请未通过 5 user_id为空 6 open_id为空 7用户不存在 8用户信息不一致 9联系人为空 10联系电话为空 11快递机构id为空 12快递单号为空\n" +
                    "  \"msg\": \"提示消息\"\n" +
                    "}")
    })
    public Result updateReturnGoods(
            @RequestBody
            @ApiParam(value ="{\n" +
                    "  \"the_order_id\": 订单id,\n" +
                    "  \"user_id\": 用户id,\n" +
                    "  \"open_id\": \"用户openid\",\n" +
                    "  \"courier_id\": 快递机构id,\n" +
                    "  \"courier_code\": \"快递单号\",\n" +
                    "  \"contact_name\": \"联系人名称\",\n" +
                    "  \"contact_mobile\": \"联系人电话\"\n" +
                    "}") Map<String,Object> reqMap){
        if(null == reqMap || reqMap.isEmpty()){
            return  Results.failure();
        }
        return returnGoodsService.updateReturnGoods(reqMap);
    }



    /**
     * 获取我的订单状态信息
     * @return
     */
    @RequestMapping(value = "/getorderstatus.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取我的订单状态信息", notes = " code 退货状态0:申请中1:同意退货2:用户已提交快递信息3:拒绝退货4:统确认后退货完成 ,99该订单未退过货")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "openId", value = "用户openId", example = "oEoCV5OzMI4wZELW4QWfi2iJ8x4o", paramType = "query", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "userId", value = "用户userId", example = "1", paramType = "query", required = true, dataType ="Integer"),
                    @ApiImplicitParam(name = "order_id", value = "用户order_id", example = "304", paramType = "query", required = true, dataType ="Integer"),
            }
    )
    public Result getMyOrderStatus(String openId,Integer userId,Integer order_id){
        return returnGoodsService.getMyOrderStatus(openId,userId,order_id);
    }



}
